package info.uno;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

public class UnoSocket 
{
	
	private String currentServer;
	private String nick = "Pat_Uno";
	private String channel = "#unoo";
	private PrintWriter pw;
	
	private Board b;
	
	private UnoSocket thisSocket = this;
	
	private boolean gameInit = false;
	private boolean gameStarted = false;
	
	private ArrayList<String> players = new ArrayList<String>();
	
	public UnoSocket()
	{
		try 
		{
			Socket skt = new Socket("irc.thinstack.net",6667);
			BufferedReader br = new BufferedReader(new InputStreamReader(skt.getInputStream()));
			pw = new PrintWriter(skt.getOutputStream(),true);
			
			while(true)
			{
				String str = br.readLine();
				
				
				if(!gameInit)
				{
					if(str.indexOf("NOTICE * :*** Looking up your hostname") != -1)
					{
						currentServer = str.substring(1, str.indexOf(" "));
						System.out.println("Connecting....");
					}
					else if(str.indexOf("Checking Ident") != -1)
					{
						pw.println("NICK "+nick);
						pw.println("USER "+nick+" 8 * : Bot Bot");
						System.out.println("Sending bot user information....");
					}
					else if(str.indexOf("376") != -1)
					{
						pw.println("JOIN "+channel);
						System.out.println("Joining channel....");
					}
					else if(str.indexOf("PING") != -1)
					{
						pw.println("PONG :" + currentServer);
						System.out.println("Pong");
					}
					else if(parseMessageNoNick(channel,str).equals("!unohelp"))
					{
						sendMsg(channel,"Commands:");
						sendMsg(channel,"!unoo - Initilizes the Uno game");
						sendMsg(channel,"!startt - Starts the Uno game");
						sendMsg(channel,"!stopp - Stops the current Uno game");
						sendMsg(channel,"join - Joins an uno game that has been initilized");
					}
					else if(parseMessageNoNick(channel,str).equals("!unoo"))
					{
						sendMsg(channel,"0,1 Uno Version 1.0 by Pat");
						sendMsg(channel,"Waiting for people to join uno...");
						gameInit = true;
					}
				}
				else
				{
					if(parseMessageNoNick(channel,str).equals("join") && !gameStarted)
					{
						sendMsg(channel,parseNick(str) + " has joined the game!");
						
						this.players.add(parseNick(str));
					}
					else if(parseMessageNoNick(channel,str).equals("!startt") && !gameStarted)
					{
						gameStarted = true;
						
						b = new Board(players);
						String[] pl = b.getCards();
						
						for(int i = 0; i < pl.length; i++)
						{
							sendNotice(players.get(i),pl[i]);
						}

						
						sendMsg(channel,"Current Card: " + b.currentCard.getLabel());
						sendMsg(channel,"Current Player: " + b.currentPlayer);
					}
					else if(parseMessageNoNick(channel,str).equals("!stopp"))
					{
						sendMsg(channel,"Uno Stopped!");
						players = new ArrayList<String>();
						gameInit = false;
						gameStarted = false;
					}
					else if(parseMessageNoNick(channel,str).equals("nm"))
					{
						int index = 0;
						
						for(int i = 0; i < players.size(); i++)
						{
							if(players.get(i).equals(parseNick(str)))
							{
								index = i;
							}
						}
						
						Card.sort(b.getPlayersCards().get(index));
						
						String[] pl = b.getCards();
						
						sendNotice(players.get(index),pl[index]);
					}
					else if(parseMessageNoNick(channel,str).substring(0,2).equals("pl") && parseNick(str).equals(b.getCurrentPlayer()))
					{
						Card tc = Card.stringToCard(parseMessageNoNick(channel,str).substring(3));
						
						System.out.println(tc.getLabel());
						int selectCardType = tc.getType();
						
						if(selectCardType != -1)
						{
							if(Card.compareCards(b.getCurrentCard(), tc))
							{
								int playerID = 0;
							
								for(int i = 0; i < players.size(); i++)
								{
									if(players.get(i).equals(parseNick(str)))
									{
										playerID = i;
									}
								}
								System.out.println("pid: " + playerID);
								
								
								for(int i = 0; i < b.getPlayersCards().get(playerID).size(); i++)
								{
									System.out.print(((Card) b.getPlayersCards().get(playerID).get(i)).getCardName() + " : ");
								}
								
								Card tempCard = Card.findCardTypeInArrayList(b.getPlayersCards().get(playerID), tc.getCardName());
								
								System.out.println(tc.getCardName());
								
								if(Card.isValidCard(tempCard))
								{
									System.out.println(tc.getCardName());
								
									System.out.println("Temp Card " + tempCard.getLabel() + " : " + tempCard.getType());
								
									String oldPlayer = b.currentPlayer;
								
									b.addCardToDeck(tempCard);
									b.getPlayersCards().get(playerID).remove(tempCard);
									b.currentCard = tempCard;
									b.currentPlayer = b.getNextPlayer();
								
									sendMsg(channel,oldPlayer + " played " + tc.getLabel() + " to " + b.getCurrentPlayer());
									
									for(int i = 0; i < players.size(); i++)
									{
										if(players.get(i).equals(b.currentPlayer))
										{
											playerID = i;
										}
									}
								
									sendNotice(b.currentPlayer,b.getCards()[playerID]);
								}
								else
								{
									sendNotice(b.currentPlayer,"Not a valid card");
								}
							}
							else
							{
								sendNotice(b.currentPlayer, "Try again!");
							}
						}
						else
						{
							sendNotice(b.currentPlayer, "Invalid Card");
						}
					}
				}
			}
		} 
		catch (UnknownHostException e) 
		{
			e.printStackTrace();
		}
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	public String parseMessage(String chan, String msg)
	{
		return msg.substring(1,msg.indexOf("!")) + " " + msg.substring(msg.indexOf(chan)+chan.length() + 2);
	}
	
	public String parseNick(String msg)
	{
		return msg.substring(1,msg.indexOf("!"));
	}
	
	public String parseMessageNoNick(String chan, String msg)
	{
		try
		{
			return msg.substring(msg.indexOf(chan) + chan.length() + 2);
		}
		catch(StringIndexOutOfBoundsException e)
		{
			
		}
		
		return "";
	}
	
	public void sendNotice(String chan, String msg)
	{
		pw.println("NOTICE "+chan+" :"+msg);
	}
	
	public void sendMsg(String chan, String msg)
	{
		pw.println("PRIVMSG "+chan+" :"+msg);
	}
	
	public void sendMsg(String chan, String[] msg)
	{
		for(int i = 0; i < msg.length; i++)
		{
			pw.println("PRIVMSG "+chan+" :"+msg[i]);
		}
	}
}
